/**
 *  筱宇
 *  常用工具封装
 */
import Vue from 'vue';
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from "element-plus";
//删除消息
export const message = async (val) => {
    let isOk = await ElMessageBox.confirm(
        `是否${val || '删除'}?`,
        "提示",
        {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
        }
    ).catch((err) => err);
    // 如果用户确认删除 则返回字符串 confirm
    // 如果用户取消了删除 则返回字符串cancel
    return isOk === 'confirm' ? true : false;
}
window.$message = message

//文件下载
export const download = (res, name) => {
    const blob = new Blob([res]);
    const url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
    const a = document.createElement("a");
    a.href = url;
    a.setAttribute("download", name);
    a.click();
    // 释放这个临时的对象url
    window.URL.revokeObjectURL(url);
}


//复制
export const copyText = (text) => {
    // navigator.clipboard.writeText(text);
    handleCopyValue(text)
        .then(() => {
            ElMessage.success("复制成功:" + text);
        })
        .catch(() => {
            ElMessage.warning("自动复制失败，请手动复制:" + text);
        });
}
function handleCopyValue(text) {
    //浏览器禁用了非安全域的 navigator.clipboard 对象
    //在线上环境会报错 TypeError: Cannot read properties of undefined (reading 'writeText')
    if (!navigator.clipboard && window.isSecureContext) {
        return navigator.clipboard.writeText(text);
    } else {
        // 判断是否支持拷贝
        if (!document.execCommand("copy")) return Promise.reject();
        // 创建标签，并隐藏
        const textArea = document.createElement("textarea");
        textArea.style.position = "fixed";
        textArea.style.top = textArea.style.left = "-100vh";
        textArea.style.opacity = "0";
        textArea.value = text;
        document.body.appendChild(textArea);
        // 聚焦、复制
        textArea.focus();
        textArea.select();
        return new Promise((resolve, reject) => {
            // 不知为何，必须写这个三目，不然copy不上
            document.execCommand("copy") ? resolve() : reject();
            textArea.remove();
        });
    }
}

// 全屏事件
// 用法----------------------
// const fullScreen = () => {
//     handleFullScreen();
// };
// 用法----------------------
const fullscreen = ref(false);
export const handleFullScreen = () => {
    const element = document.documentElement;
    if (fullscreen.value) {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    } else {
        if (element.requestFullscreen) {
            element.requestFullscreen();
        } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.msRequestFullscreen) {
            // IE11
            element.msRequestFullscreen();
        }
    }
    fullscreen.value = !fullscreen.value;
};